﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.BusinessObject;
using pwblog.Entities;

namespace pwblog.Web.Management
{
    public partial class MangeAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillAccountsGrid();
            }
        }

        /// <summary>
        /// Tải dữ liệu lên GridView
        /// </summary>
        protected void FillAccountsGrid()
        {
            var accounts = new AccountBo().GetAccounts();
            if (accounts != null)
            {
                gvAccounts.DataSource = accounts;
                gvAccounts.DataBind();
            }
        }

        /// <summary>
        /// Sự kiện tải dữ liệu vào một dòng trong GridView
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewRowEventArgs</param>
        protected void gvAccounts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlRoles = ((DropDownList)e.Row.FindControl("ddlRoles"));
                if (ddlRoles != null)
                {
                    if (gvAccounts.DataKeys[e.Row.RowIndex].Values[1] != null)
                    {
                        ddlRoles.SelectedValue = gvAccounts.DataKeys[e.Row.RowIndex].Values[1].ToString().Trim();
                    }
                }
            }
        }

        /// <summary>
        /// xử lí sự kiện gvCategories_RowCommand khi click "Insert"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewRowEventArgs</param>
        protected void gvAccounts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName.Equals("Insert"))
            {
                string userName = ((TextBox)gvAccounts.FooterRow.FindControl("txtUserName")).Text.Trim().ToLower();
                string password = ((TextBox)gvAccounts.FooterRow.FindControl("txtPassword")).Text.Trim();
                string birtday = ((TextBox)gvAccounts.FooterRow.FindControl("txtBirthday")).Text.Trim();
                string address = ((TextBox)gvAccounts.FooterRow.FindControl("txtAddress")).Text.Trim();
                string roles = ((DropDownList)gvAccounts.FooterRow.FindControl("ddlRoles")).SelectedValue;
                int numberOfArticle = 0;

                Account newAccount = new Account(){
                    UserName = userName,
                    Password = password,
                    Birthday = birtday,
                    Address = address,
                    Roles = roles,
                    NumberOfArticle = numberOfArticle
                };
                if (new AccountBo().InsertAccout(newAccount))
                {
                    // thanh cong
                }
                else
                {
                    // that bat
                }
                FillAccountsGrid();
            }
        }

        /// <summary>
        /// Sử lí sự kiện gvCategories_RowEditing khi click vào "Edit"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewEditEventArgs</param>
        protected void gvAccounts_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvAccounts.EditIndex = e.NewEditIndex;
            FillAccountsGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowDeleting khi click vào "Delete"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewDeleteEventArgs</param>
        protected void gvAccounts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string userName = gvAccounts.DataKeys[e.RowIndex].Values[0].ToString();
            if (new AccountBo().DeleteAccount(userName))
            {
                // thanh cong
            }
            else
            {
                // that bai
            }
            FillAccountsGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowUpdating khi click "Update"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewUpdateEventArgs</param>
        protected void gvAccounts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string userName = gvAccounts.DataKeys[e.RowIndex].Values[0].ToString();
            string birthday = ((TextBox)gvAccounts.Rows[e.RowIndex].FindControl("txtBirthday")).Text.Trim();
            string address = ((TextBox)gvAccounts.Rows[e.RowIndex].FindControl("txtAddress")).Text.Trim();
            string roles = ((DropDownList)gvAccounts.Rows[e.RowIndex].FindControl("ddlRoles")).SelectedValue;
            if (new AccountBo().UpdateAccount(userName, birthday, address, roles))
            {
                // thanh cong
            }
            else
            {
                // that bai
            }
            gvAccounts.EditIndex = -1;
            FillAccountsGrid();
        }

        /// <summary>
        /// Xử lý sự kiện gvCategories_RowCancelingEdit khi click vào "Cancel"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewCancelEditEventArgs</param>
        protected void gvAccounts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvAccounts.EditIndex = -1;
            FillAccountsGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_PageIndexChanging khi chuyển trang
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewPageEventArgs</param>
        protected void gvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAccounts.PageIndex = e.NewPageIndex;
            FillAccountsGrid();
        }
    }
}